package com.unicom.si.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.unicom.si.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
// 开启spring的@Transactional注解
@EnableTransactionManagement(proxyTargetClass = true, order = 3)
public class DataSourceConfig {

    @Value("${mysql.database.driverName}")
    private String className;

    @Value("${mysql.database.url}")
    private String url = null;

    @Value("${mysql.database.user}")
    private String username;

    @Value("${mysql.database.password}")
    private String password;

    @Value("${initialSize:10}")
    private int initialSize;

    @Value("${minIdle:10}")
    private int minIdle;

    @Value("${maxActive:20}")
    private int maxActive;

    @Value("${maxWait:60000}")
    private long maxWait;

    @Value("${timeBetweenEvictionRunsMillis:60000}")
    private long timeBetweenEvictionRunsMillis;

    @Value("${minEvictableIdleTimeMillis:300000}")
    private long minEvictableIdleTimeMillis;

    @Primary // 标明这个是首选
    @Bean(name = "dataSource")
    public DruidDataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setEnable(true);

        dataSource.setDriverClassName(className);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        //数据库连接池初始值，最小值，最大值
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxActive(maxActive);
        //配置获取连接等待超时的时间
        dataSource.setMaxWait(maxWait);
        //间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        dataSource.setTimeBetweenConnectErrorMillis(timeBetweenEvictionRunsMillis);
        //一个连接在池中最小生存的时间，单位是毫秒
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

        try {
            dataSource.setFilters("stat");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dataSource;
    }

    //定义spring的事务
    @Bean
    @Autowired
    // 自动注入DataSource对象，
    // 如果DataSource的对象没有@Primary，这里需要@Qualifier注解，表明要注入哪一个DataSource
    // 或者使用@Qualifier标明要注入哪个DataSource
    public PlatformTransactionManager initSpringTransactionManager( DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);

        return transactionManager;
    }


//    @Bean("configProperties")
//    public PropertiesFactoryBean getProperties() {
//        PropertiesFactoryBean properties = new PropertiesFactoryBean();
//        Resource res = new ClassPathResource("datasource.properties");
//        properties.setLocations(res);
//
//        return properties;
//    }

//    @Bean("propertyConfigurer")
//    public PreferencesPlaceholderConfigurer propertiesInit() {
//        PreferencesPlaceholderConfigurer preConfig = new PreferencesPlaceholderConfigurer();
//        Resource res = new ClassPathResource("datasource.properties");
//        preConfig.setLocation(res);
//
//        return preConfig;
//    }


}
